home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / gethostn.c < prev    next >
C/C++ Source or Header  |  1993-10-11  |  908b  |  53 lines

  1. /* gethostname -- for now, fake by looking in environment */
  2. /* (written by Eric R. Smith, placed in the public domain) */
  3.  
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <support.h>
  10.  
  11. #define MAXLEN 127
  12.  
  13. int
  14. gethostname(buf, len)
  15.     char *buf;
  16.     size_t len;
  17. {
  18.     char *foo = 0;
  19.     char xbuf[MAXLEN+1];
  20.     int fd, r;
  21.  
  22. #if 0
  23.     if (!foo) {
  24. #endif
  25. /* try looking for the file /etc/hostname; if it's present,
  26.  * it contains the name, otherwise we try the environment
  27.  */
  28.         fd = open("/etc/hostname", O_RDONLY);
  29.         if (fd >= 0) {
  30.             r = read(fd, xbuf, MAXLEN);
  31.             if (r > 0) {
  32.                 xbuf[r] = 0;
  33.                 foo = xbuf;
  34.                 while (*foo) {
  35.                     if (*foo == '\r' || *foo == '\n')
  36.                         *foo = 0;
  37.                     foo++;
  38.                 }
  39.                 foo = xbuf;
  40.             }
  41.             close(fd);
  42.         }
  43. #if 0
  44.     }
  45. #endif
  46.     if (!foo)
  47.         foo = getenv("HOSTNAME");
  48.     if (!foo)
  49.         foo = "unknown";
  50.     strncpy(buf, foo, len);
  51.     return 0;
  52. }
  53.